//
//  Notice+Extension.swift
//  Library_y
//
//  Created by JC on 2023/6/6.
//

import Foundation

/// 发送通知类
final public class Notice { }

/// 自定义通知
extension Notice {
    
    /// 发送通知
    ///
    /// - Parameters:
    ///   - name: 通知标识
    ///   - value: 发送通知的值
    public static func post(_ value: Any? = nil, signName: String) {
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: signName), object: value)
    }
    
    /// 接收通知
    ///
    /// - Parameters:
    ///   - action: 触发事件
    ///   - signName: 通知标识
    public static func addObserver(target: Any, action: Selector, signName: String, object: Any? = nil) {
        NotificationCenter.default.addObserver(target, selector: action, name: NSNotification.Name(rawValue: signName), object: object)
    }
    
    
    /// 移除通知
    ///
    /// - Parameters:=
    ///   - signName: 通知标识
    public static func removeNotice(target: Any, signName: String) {
        NotificationCenter.default.removeObserver(target, name: NSNotification.Name(rawValue: signName), object: nil)
    }
    
    /// 移除所有通知
    public static func removeAllNotice(target: Any) {
        NotificationCenter.default.removeObserver(target)
    }
}

/// 系统通知
extension Notice {
    
    /// 发送系统通知
    ///
    /// - Parameters:
    ///   - name: 通知标识
    ///   - value: 发送通知的值
    public static func post(_ value: Any? = nil, signName: NSNotification.Name) {
        NotificationCenter.default.post(name: signName, object: value)
    }
    
    /// 接收系统通知
    ///
    /// - Parameters:
    ///   - action: 触发事件
    ///   - signName: 通知标识
    public static func addObserver(target: Any, action: Selector, signName: NSNotification.Name, object: Any? = nil) {
        NotificationCenter.default.addObserver(target, selector: action, name:signName, object: object)
    }
    
    
    /// 移除系统通知
    ///
    /// - Parameters:=
    ///   - signName: 通知标识
    public static func removeNotice(target: Any, signName: NSNotification.Name) {
        NotificationCenter.default.removeObserver(target, name: signName, object: nil)
    }
}
